<template>
    <div class="main">
        <div class="login-box">
            <input type="text" placeholder="请输入账号" v-model="account">
            <input type="password" placeholder="请输入密码" v-model="pswd">
            <button class="dis-b" type="button" @click="login">登陆</button>
        </div>
        <toast width="60%" v-model="toast" type="text" :time="800" is-show-mask position="default">{{ msg }}</toast>
    </div>
</template>

<script>
    import util from './../../fetch/util.js'
    import store from '@/vuex/store'

    export default {
        name: "login",
        data(){
            return {
                account:'',
                pswd: '',
                toast: false,
                msg: '',
            }
        },
        store,
        methods: {
            login() {
                if(this.account == '' || this.pswd == ''){
                    this.msg = '账号或密码不能为空';
                    this.toast = true;
                    return false;
                }
                let params = {
                    id: this.account,
                    Pass: this.pswd
                };

                util.fetchGet(util.path.login, params).then((data)=>{
                    if(data.code === 0){
                        this.$store.commit('setUser', data.data);

                        localStorage.setItem('userInfo', JSON.stringify(data.data));
                        localStorage.setItem('loginState', '1');
                        localStorage.setItem('loginTime', new Date().getTime());
                        this.$router.push({
                            name: 'home'
                        });

                    }else{
                        this.msg = data.msg;
                        this.toast = true;
                    }
                })
            }
        }
    }
</script>

<style scoped>

    .login-box{
        margin-top: 200px;
    }
    input{
        width: 90%;
        margin-left: 5%;
        box-sizing: border-box;
        border-bottom: #ddd 1px solid;
        margin-bottom: 10px;
        background: transparent;
        line-height: 36px;
    }
    button{
        width: 90%;
        margin: 20px auto;
        background: #2baee9;
        color: #fff;
        line-height: 40px;
        border-radius: 4px;
    }
</style>
